package com.ptswitch.material.components.test;

import java.util.List;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.apache.tapestry5.tree.DefaultTreeModel;
import org.apache.tapestry5.tree.TreeModel;
import org.apache.tapestry5.tree.TreeModelAdapter;
import org.hxzon.util.Dt;

import com.ptswitch.material.model.security.Permission;
import com.ptswitch.material.service.PermissionService;
import com.ptswitch.material.util.security.PermissionUtil;

@Import(library = { "SelectPermission.js" })
public class SelectPermission {

    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    @Property
    private String myLabel;
    @Parameter
    private Permission value;
    //@Property
    private String valueIdSelect;
    @Property
    private Permission item;

    //-------------
    private String clientId;

    @Inject
    private PermissionService permissionSer;
    //
    @Inject
    private ComponentResources resources;

    @Inject
    private JavaScriptSupport jsSupport;

    //valueIdSelect
    public void setValueIdSelect(String permissionId) {
        this.valueIdSelect = permissionId;
        this.value = permissionSer.findById(Dt.toLong(valueIdSelect, 0));
    }

    public String getValueIdSelect() {
        return valueIdSelect;
    }

    //permissionModel
    public TreeModel<Permission> getPermissionModel() {
        ValueEncoder<Permission> encoder = new ValueEncoder<Permission>() {

            @Override
            public String toClient(Permission p) {
                return String.valueOf(p.getId());
            }

            @Override
            public Permission toValue(String clientValue) {
                return permissionSer.findById(Dt.toLong(clientValue, 0));
            }
        };
        Permission root = PermissionUtil.All;
        return new DefaultTreeModel<Permission>(encoder, new PermissionAdapter(), root);

    }

    //------------
    private class PermissionAdapter implements TreeModelAdapter<Permission> {

        @Override
        public boolean isLeaf(Permission p) {
            return p.getChildren().isEmpty();
        }

        @Override
        public boolean hasChildren(Permission p) {
            return !p.getChildren().isEmpty();
        }

        @Override
        public List<Permission> getChildren(Permission p) {
            return p.getChildren();
        }

        @Override
        public String getLabel(Permission p) {
            return p.getDisplayName();
        }

    }

    public final String getClientId() {
        if (clientId == null) {
            clientId = jsSupport.allocateClientId(resources);
        }
        return clientId;
    }
}
